#!/bin/bash
#
# Copyright 2015-2017 Adrian DC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

# === Standalone Source Helper ===
# source <(curl -Ls https://github.com/AdrianDC/android_development_shell_tools/raw/master/android_devices.rc)
# source <(curl -Ls https://github.com/AdrianDC/android_development_shell_tools/raw/master/android_adb_tools.rc)
# source <(curl -Ls https://github.com/AdrianDC/android_development_shell_tools/raw/master/android_adb_flash.rc)

# === ADB Bootimage Dumper ===
function adbbootdump()
{
  # Usage: adbbootdump (Dump bootimage from device)

  # Variables
  local cmdaddr;
  local cmdsize;
  local kernel;

  # Dump bootimage from device
  kernel=$(androiddevicestarget boot);
  cmdaddr=$(adb shell od -j 152 -N 4 -tx4 -An "${kernel}" \
          | tr -d ' ' | cut -c 2-9);
  cmdsize=$(adb shell od -j 164 -N 4 -tx4 -An "${kernel}" \
          | tr -d ' ' | cut -c 2-9);
  if [ ! -z "${cmdaddr}" ] && [ "$(echo "${cmdaddr}" | wc -l)" -eq 1 ] && \
     [ ! -z "${cmdsize}" ] && [ "$(echo "${cmdsize}" | wc -l)" -eq 1 ]; then
    local bootsize=$((16#${cmdaddr}+16#${cmdsize}));
    adbsu "dd if=${kernel} of=/sdcard/boot.img skip=0 bs=${bootsize} count=1";
    adb pull /sdcard/boot.img;
    adbsu 'rm -f /sdcard/boot.img';
  else
    adbsu "dd if=${kernel} of=/sdcard/boot.dump";
    adb pull /sdcard/boot.dump;
    adbsu 'rm -f /sdcard/boot.dump';
    adbbootcut boot.dump;
  fi;
}

# === ADB Bootimage Resizer ===
function adbbootcut()
{
  # Usage
  if [ -z "${1}" ] || [ ! -f "${1}" ]; then
    echo '';
    echo ' Usage: adbbootcut <file_path> (Trim bootimage dump)';
    echo '';
    return;
  fi;

  # Variables
  local bootsize;
  local cmdaddr;
  local cmdsize;

  # Trim bootimage dump
  cmdaddr=$(od -j 152 -N 4 -tx4 -An "${1}" \
          | cut -c 2-9);
  cmdsize=$(od -j 164 -N 4 -tx4 -An "${1}" \
          | cut -c 2-9);
  bootsize=$((16#${cmdaddr}+16#${cmdsize}));
  dd if="${1}" of=boot.img skip=0 bs="${bootsize}" count=1;
}

# === ADB TWRP Installer ===
function adbrecoveryinstall()
{
  # Usage
  if [ -z "${1}" ] || [ ! -f "${1}" ]; then
    echo '';
    echo ' Usage: adbrecoveryinstall <file_path> (Inject and reboot recovery)';
    echo '';
    return;
  fi;

  # Variables
  local filename;

  # Inject and reboot recovery
  filename=$(basename "${1}");
  adbro;
  adb shell mkdir -p /sdcard/;
  adb push "${1}" "/sdcard/${filename}";
  adb shell 'mkdir -p /cache/recovery';
  adb shell "echo 'install /sdcard/${filename}' > /cache/recovery/command";
  adb shell 'cat /cache/recovery/command';
  adb reboot recovery;
}
